package name.sakanacatcher.service;

import name.sakanacatcher.entity.PositionOption;
import name.sakanacatcher.entity.PositionOptionPosition;
import name.sakanacatcher.entity.PositionOptionType;
import name.sakanacatcher.repository.POPositionRepository;
import name.sakanacatcher.repository.POTypeRepository;
import name.sakanacatcher.repository.PositionOptionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PositionOptionTypeService {
    @Autowired
    PositionOptionRepository positionOptionRepository;

    @Autowired
    POPositionRepository positionRepository;

    @Autowired
    POTypeRepository typeRepository;

    public PositionOptionRepository getPositionOptionTypeRepository() {
        return positionOptionRepository;
    }

    public List<Map<String,Object>> getPositionOption(){
        List<PositionOptionType> typeList = typeRepository.findAll();
        List<PositionOption> posList = positionOptionRepository.findAll();
        List<String> types = new ArrayList<>();
        List<Map<String,Object>> res = new ArrayList<Map<String,Object>>();
        for(PositionOptionType p:typeList){
            if (!types.contains(p.getName())){
                types.add(p.getName());
                Map<String,Object> first = new HashMap<>();
                first.put("id",p.getId());
                first.put("name",p.getName());
                res.add(first);
            }
        }
        for(Map<String,Object> m:res){
            List<Map<String,Object>> second = new ArrayList<>();
            for(PositionOption p:posList){
                if(p.getType().getName().equals(m.get("name"))){
                    Map<String,Object> children = new HashMap<>();
                    children.put("id",p.getId());
                    children.put("name",p.getPos().getName());
                    second.add(children);
                }
            }
            m.put("children",second);
        }
        return res;
    }

    @Transactional
    public boolean deletePosition(Map<String,Object> data){
        String typeName = (String) data.get("typeName");
        String positionName = (String) data.get("positionName");
        PositionOption positionOption = positionOptionRepository.getPositionOptionByTypeNameAndAndPosName(typeName, positionName);
        positionOption.setType(null);
        positionOption.setPos(null);
        positionOptionRepository.delete(positionOption);
        return !positionOptionRepository.existsById(positionOption.getId());
    }

    @Transactional
    public boolean addPosition(Map<String,Object> data){
        String typeName = (String) data.get("typeName");
        String positionName = (String) data.get("positionName");
        PositionOptionPosition position;
        if(positionRepository.existsByName(positionName)) {
            position = positionRepository.getByName(positionName);
        }
        else {
            position = new PositionOptionPosition(positionName);
            position = positionRepository.save(position);
        }
        PositionOptionType type = typeRepository.getByName(typeName);
        PositionOption positionOption = new PositionOption();
        positionOption.setType(type);
        positionOption.setPos(position);
        positionOptionRepository.save(positionOption);
        return !positionOptionRepository.existsById(positionOption.getId());
    }

    @Transactional
    public boolean addType(Map<String,Object> data) {
        String typeName = (String) data.get("typeName");
        PositionOptionType type;
        if(typeRepository.existsByName(typeName)){
            type = typeRepository.getByName(typeName);
        }
        type = new PositionOptionType(typeName);
        type = typeRepository.save(type);
        return typeRepository.existsById(type.getId());
    }

    public boolean deleteType(Map<String,Object> data) {
        String typeName = (String) data.get("typeName");
        PositionOptionType type = typeRepository.getByName(typeName);
        List<PositionOption> positionOptions = positionOptionRepository.getPositionOptionByTypeName(typeName);
        for(PositionOption p:positionOptions){
            p.setPos(null);
            positionOptionRepository.delete(p);
        }
        return typeRepository.existsByName(typeName);
    }
}
